#include <gtest/gtest.h>
#include <fstream>
#include <memory>
#include <stdexcept>
#include "raii.cpp"

// 测试构造函数和文件打开
TEST(FileManagerTest, ConstructorOpensFile) {
    const std::string filename = "test_file.txt";
    {
        FileManager manager(filename);
        std::ifstream testFile(filename);
        EXPECT_TRUE(testFile.is_open());
    }
    // 文件应在析构时关闭
    std::ifstream testFile(filename);
    EXPECT_FALSE(testFile.is_open());
}

// 测试无法打开文件时的异常
TEST(FileManagerTest, ConstructorThrowsOnFailure) {
    const std::string invalidFilename = "/invalid/path/test_file.txt";
    EXPECT_THROW(FileManager manager(invalidFilename), std::runtime_error);
}

// 测试析构函数关闭文件
TEST(FileManagerTest, DestructorClosesFile) {
    const std::string filename = "test_file.txt";
    {
        FileManager manager(filename);
        std::ifstream testFile(filename);
        EXPECT_TRUE(testFile.is_open());
    }
    std::ifstream testFile(filename);
    EXPECT_FALSE(testFile.is_open());
}

// 测试移动语义
TEST(FileManagerTest, MoveConstructor) {
    const std::string filename = "test_file.txt";
    FileManager manager1(filename);
    FileManager manager2 = std::move(manager1);
    std::ifstream testFile(filename);
    EXPECT_TRUE(testFile.is_open());
}

// 测试写入功能
TEST(FileManagerTest, WriteContent) {
    const std::string filename = "test_file.txt";
    const std::string content = "Hello, World!";
    {
        FileManager manager(filename);
        manager.write(content);
    }
    std::ifstream testFile(filename);
    std::string line;
    std::getline(testFile, line);
    EXPECT_EQ(line, content);
}

// 测试禁用复制
TEST(FileManagerTest, CopyIsDisabled) {
    const std::string filename = "test_file.txt";
    FileManager manager1(filename);
    // 以下代码应导致编译错误，但无法在运行时测试
    // FileManager manager2 = manager1;
    // FileManager manager3;
    // manager3 = manager1;
    EXPECT_TRUE(true); // 占位符，表示测试通过
}

int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}